package com.capgemini.fs.app.ekms.service.implementations;

import java.util.List;
import java.util.Set;

import com.capgemini.fs.app.ekms.common.Constants;
import com.capgemini.fs.app.ekms.model.Forum;
import com.capgemini.fs.app.ekms.model.Role;
import com.capgemini.fs.app.ekms.model.User;
import com.capgemini.fs.app.ekms.persistence.PersistenceService;
import com.capgemini.fs.app.ekms.persistence.dao.ForumDAO;
import com.capgemini.fs.app.ekms.persistence.dao.UserDAO;
import com.capgemini.fs.app.ekms.service.UserService;
import com.capgemini.fs.app.ekms.service.util.SHAPasswordEncoder;

public class UserServiceImpl implements UserService {


	private PersistenceService persistenceService = null;

	public PersistenceService getPersistenceService() {
		return persistenceService;
	}
	public boolean isValidUser(String username, String password) {
		boolean result = false;
		try {
			if (getUserByName(username) != null) {
				result = getUserByName(username).getPassword().equals(
						new SHAPasswordEncoder().encode(password));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}


	public void setPersistenceService(PersistenceService persistenceService) {
		this.persistenceService = persistenceService;
	}

	public UserDAO getUserDAO() {
		return persistenceService.getUserDAO();
	}

	public void createUser(User user) {
		getUserDAO().addUser(user);
	}

	public User getUserByName(String name) {
		return getUserDAO().getUserByName(name);

	}

	public void addRoleToUser(User user, Role role) {
		getUserDAO().addRoleToUser(user, role);
	}

	public boolean verifyUser(User user) {
		User tempUser = getUserDAO().getUserByName(user.getUserName());
		if (tempUser != null
				& user.getPassword().equals(tempUser.getPassword())) {
			return true;
		} else {
			return false;
		}
	}

	public void updateUser(User user) {
		getUserDAO().evict(user);
		User exist = getUserDAO().findByPrimaryKey(User.class, user.getId());

		exist.setCreateDate(user.getCreateDate());
		exist.setCreator(user.getCreator());
		exist.setEmail(user.getEmail());
		exist.setGroups(user.getGroups());
		exist.setLastModifier(user.getLastModifier());
		exist.setLastUpdateDate(user.getLastUpdateDate());
		exist.setPassword(user.getPassword());
		exist.setRoles(user.getRoles());
		exist.setStatus(user.getStatus());
		exist.setUserName(user.getUserName());

		getUserDAO().update(exist);

	}

	public User getUserById(Integer id) {
		return getUserDAO().findByPrimaryKey(User.class, id);
	}

	public List<User> getAllUsers() {
		return getUserDAO().findAll(User.class);
	}

	public List<User> queryUser(String firstName, String lastName,
			String userName, Integer status) {

		return getPersistenceService().getUserDAO().queryUser(firstName,
				lastName, userName, status);
	}

	public List<User> queryUser(String firstName, String lastName,
			String userName, Integer status, String email, String phoneNumber,
			Integer memberType, List<String> roles, List<String> groups) {
		return getPersistenceService().getUserDAO().queryUser(firstName,
				lastName, userName, status, email, phoneNumber, memberType,
				roles, groups);
	}

	// Leave Forum
	public void leaveForum(Integer userId, Integer forumId) {
	}

	// Join Forum

	public Integer joinForum(Integer userId, Integer forumId) {
		Integer result = null;
		ForumDAO forumDAO = getPersistenceService().getForumDAO();
		Forum forum = forumDAO.get(Forum.class, forumId);
		User user = getUserById(userId);
		if (forum == null || user == null) {
			result = Constants.USER_OR_FORUM_NOT_EXIST_ERROR;
		} else {
			if(true==isForumMember(user, forum)){
				result=Constants.MEMBER_EXISIT_ERROR;
			}else{
		    forum.getMembers().add(user);
		    getPersistenceService().getForumDAO().update(forum);
		    result=Constants.ADD_MEMBER_SUCC;
			}
		}
		return result;
	}

	public boolean isForumMember(User user, Forum forum) {
		boolean result = false;
		Set<User> forumMembers = forum.getMembers();
		if (0 == forumMembers.size()) {
			result = false;
		} else {
			for (User forumMember : forumMembers) {
				if (forumMember.getId() == user.getId()) {
					result = true;
					break;
				} else {
					result = false;
				}
			}
		}
		return result;
	}

}
